// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import 'lib-flexible/flexible.js'
import less from 'less'
import NProgress from 'nprogress' //引入自定义css是为了覆盖掉默认的进度条的颜色
import './assets/css/nprogress.css'
import axios from './http/request'
import baseComponents from './components'


Vue.prototype.$axios = axios

Vue.use(baseComponents)
Vue.use(less)
Vue.config.productionTip = false


NProgress.configure({
  easing: 'ease',  // 动画方式    
  speed: 500,  // 递增进度条的速度    
  showSpinner: false, // 是否显示加载ico    
  trickleSpeed: 200, // 自动递增间隔    
  minimum: 0.3 // 初始化时的最小百分比
})

router.beforeEach((to, from , next) => {
  /* 路由发生变化修改页面title */
  if (to.meta.title) {
      document.title = to.meta.title
  }
  document.body.scrollTop = 0;
  
  // 每次切换页面时，调用进度条
  NProgress.start();
  // 对路由进行验证    
  if(to.matched.some( m => m.meta.auth)){  // 某路由需要登录权限
    const ifLogin = localStorage.getItem('isLogin') 
    if(!ifLogin) { // 未登陆
        next({path:'/login'})
        NProgress.done()
    }else{
        next()
    }
  }else{
      next()
  }
});

router.afterEach(() => {  
  // 在即将进入新的页面组件前，关闭掉进度条
  NProgress.done()
})



/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
